package com.jucai.pppeiyouclient.view.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jucai.pppeiyouclient.MainActivity;
import com.jucai.pppeiyouclient.adapter.SkillGroupAdapter;
import com.jucai.pppeiyouclient.base.BaseActivity;

import org.json.JSONArray;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.jucai.pppeiyouclient.R;
import com.jucai.pppeiyouclient.bean.OrderBean;
import com.jucai.pppeiyouclient.bean.SkillGroupBean;
import com.jucai.pppeiyouclient.comm.CommonRequestNetData;
import com.jucai.pppeiyouclient.utils.ActivityCollector;
import com.jucai.pppeiyouclient.utils.LogUtils;

/**
 * 技能列表
 */
public class SkillListActivity extends BaseActivity {
    private static final String TAG = SkillListActivity.class.getSimpleName();
    private RecyclerView skillGroupRv;
    private List<SkillGroupBean> skillGroups = new ArrayList<>();
    private SkillGroupAdapter groupAdapter;
    private View headerView, footerView;
    private TextView skillSubmit;
    private View viewNotSelect;

    @Override
    protected void initView(Bundle savedInstanceState) {
        setContentView(R.layout.common_activity_with_rv);

        groupAdapter = new SkillGroupAdapter(SkillListActivity.this, skillGroups);
        skillGroupRv = (RecyclerView) findViewById(R.id.common_view);
        skillGroupRv.setLayoutManager(new LinearLayoutManager(SkillListActivity.this, LinearLayoutManager.VERTICAL, false));
        ((LinearLayoutManager) skillGroupRv.getLayoutManager()).setReverseLayout(false);
        skillGroupRv.setItemAnimator(new DefaultItemAnimator());
        skillGroupRv.setAdapter(groupAdapter);

        headerView = LayoutInflater.from(SkillListActivity.this).inflate(R.layout.head_skill_group, skillGroupRv, false);
        footerView = LayoutInflater.from(SkillListActivity.this).inflate(R.layout.footer_skill_group, skillGroupRv, false);
        groupAdapter.setHeader(headerView);
        groupAdapter.setBottom(footerView);
        //viewNotSelect =  headerView.findViewById(R.id.rlt_not_select);
        skillSubmit = (TextView) footerView.findViewById(R.id.skills_submit);

       /* groupAdapter.setOnFilterResultListener(new SkillGroupAdapter.OnFilterResultListener() {
        @Override
        public void onResult(List<FilterAdapter.FilterBean> filterBeanList) {
            Intent intent = new Intent();
            intent.putExtra("selectedFilterBean", GsonUtils.getGsonInstance().toJson(filterBeanList));
            SkillListActivity.this.setResult(RESULT_OK, intent);
            SkillListActivity.this.finish();
        }
    });*/
    }

    @Override
    protected void initData() {
        loadNetData();
    }


    protected void loadNetData() {
        HashMap<String, String> param = new HashMap<>();
        param.put("service", "Accompany_Skill.getSkillList");

        CommonRequestNetData.getJsonArrayData(SkillListActivity.this, param, true, new CommonRequestNetData.JsonArrayCallBack() {
            @Override
            public void parseJsonArray(JSONArray jsonArray) {
                LogUtils.e(TAG + "jsonArray = " + jsonArray.toString());
                skillGroups = new Gson().fromJson(jsonArray.toString(), new TypeToken<List<SkillGroupBean>>() {
                }.getType());
                groupAdapter.setData(skillGroups);
            }
        });
    }

    @Override
    protected void setListener() {
//        viewNotSelect.setOnClickListener(this);
        skillSubmit.setOnClickListener(this);

        findViewById(R.id.common_back).setOnClickListener(this);
        findViewById(R.id.common_home_back).setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.common_back:
                ActivityCollector.removeActivity(this);
                break;

            case R.id.common_home_back:
                startActivity(new Intent(SkillListActivity.this, MainActivity.class));
                ActivityCollector.removeActivity(this);
                break;
            case R.id.skills_submit:
               /* if (groupAdapter != null && groupAdapter.getSelectedFilterBeanList().size() <= 0) {
                    ToastUtil.showToast(SkillListActivity.this, "请选择一个技能");
                    break;
                }*/
                /*Intent intent = new Intent();
                intent.putExtra("selectedFilterBean", GsonUtils.getGsonInstance().toJson(groupAdapter.getSelectedFilterBeanList()));
                SkillListActivity.this.setResult(RESULT_OK, intent);
                SkillListActivity.this.finish();*/
                break;
            /*case R.id.rlt_not_select:
                SkillListActivity.this.setResult(Activity.RESULT_OK, null);
                SkillListActivity.this.finish();
                break;*/
        }
    }
}
